%clear all

%uses as inputs the variables 'AIC','R2adj','R_2','SIC' which are generated from the main
%m-file run

%therefore run this file after the main m-file with the data in the cache or save the in sample criteria after the main run and read them in here 
clc

%the following can be left out if running with the main regression results
%still in the cache. Otherwise you need to read in these extra variables
%from the saved reg_25_bootvar data file

    %load reg_25_bootvar %this loads the other variables which are used in this file such as 'f_hat' ,'f_hatA', 'block','Number','NumberA'

AIC_model=find(AIC==min(AIC));
SIC_model=find(SIC==min(SIC));
R_2_model=find(R_2==max(R_2));
R2adj_model=find(R2adj==max(R2adj));
%pc_model=find(mean(f_hat)==min(mean(f_hat))); %need it for the best Agg model so just redefine the pc_model varaible 
pc_model=find(mean(f_hatA)==min(mean(f_hatA)));
block=5;

 diary('boot_in_sample');
for xth=1:3; % since some (3) models have the same in-sample criteria (that is as models with a time fixed effect change in their forecasts for different structural breaks but have the same regression results) 
disp([num2str(xth),' th run since we have some models with equal in-sample criteria'])
disp('best AIC as benchmark model')
f_hatA_less=f_hatA;
f_hatA_less(:,AIC_model(xth))=[9999,8888,7777,6666,5555,4444,3333,2222,1111,0000];
[Trc,Tspc,u,u_10percent,c_hansen,c_hansen_10percent,naive_p,naive_10percent,best,besthansen]=bsds_ralf_ess(f_hatA(:,AIC_model(xth)),f_hatA_less,500,block,'STATIONARY',AIC_model(xth))

disp('best SIC as benchmark model')
f_hatA_less=f_hatA;
f_hatA_less(:,SIC_model(xth))=[9999,8888,7777,6666,5555,4444,3333,2222,1111,0000];

[Trc,Tspc,u,u_10percent,c_hansen,c_hansen_10percent,naive_p,naive_10percent,best,besthansen]=bsds_ralf_ess(f_hatA(:,SIC_model(xth)),f_hatA_less,500,block,'STATIONARY',SIC_model(xth))

disp('best R2 as benchmark model')
f_hatA_less=f_hatA;
f_hatA_less(:,R_2_model(xth))=[9999,8888,7777,6666,5555,4444,3333,2222,1111,0000];

[Trc,Tspc,u,u_10percent,c_hansen,c_hansen_10percent,naive_p,naive_10percent,best,besthansen]=bsds_ralf_ess(f_hatA(:,R_2_model(xth)),f_hatA_less,500,block,'STATIONARY',R_2_model(xth))

disp('best R2adj as benchmark model')
f_hatA_less=f_hatA;
f_hatA_less(:,R2adj_model(xth))=[9999,8888,7777,6666,5555,4444,3333,2222,1111,0000];

[Trc,Tspc,u,u_10percent,c_hansen,c_hansen_10percent,naive_p,naive_10percent,best,besthansen]=bsds_ralf_ess(f_hatA(:,R2adj_model(xth)),f_hatA_less,500,block,'STATIONARY',R2adj_model(xth))
end

disp('best per capita model as benchmark model')
f_hatA_less=f_hatA;
f_hatA_less(:,pc_model)=[9999,8888,7777,6666,5555,4444,3333,2222,1111,0000];

[Trc,Tspc,u,u_10percent,c_hansen,c_hansen_10percent,naive_p,naive_10percent,best,besthansen]=bsds_ralf_ess(f_hatA(:,pc_model),f_hatA_less,500,block,'STATIONARY',pc_model)


    %these are going into table 3 MSFE
    mean(f_hat(:,AIC_model(xth)))
    mean(f_hat(:,SIC_model(xth)))
    mean(f_hat(:,R_2_model(xth)))
    mean(f_hat(:,R2adj_model(xth)))
    mean(f_hat(:,pc_model))
    
    mean(f_hatA(:,AIC_model(xth)))
    mean(f_hatA(:,SIC_model(xth)))
    mean(f_hatA(:,R_2_model(xth)))
    mean(f_hatA(:,R2adj_model(xth)))
    mean(f_hatA(:,pc_model))

    %these are going into table 4 MSFE
    mean(f_bench)
    mean(f_benchA)
    mean(f_B2b)
    mean(f_B2bA)
    mean(f_B3)
    mean(f_B3A)
    mean(f_hat(:,NumberA))
    mean(f_hatA(:,NumberA))
  %the bootstrap results from above, together with bootstrap test runs at the end of the main regression m-file, give all inputs for tables 3 & 4   

diary off